<?php
declare(strict_types=1);
/**
 * Created by PhpStorm
 * User: Administrator
 * Date: 2023/8/8
 * Time: 09:30:10
 */

namespace Service\CommodityService;

use Service\Code\Normal;
use App\Model\BbcModel\GoodsdetailsDecorate;
use Service\BaseService;

/**
 * 商品设置管理
 */
class GoodsConfigService extends BaseService
{
    /**
     * 商品设置页详情
     * @param $param
     * @return array
     */
    public function goodsConfigDetails($param)
    {
        $info = GoodsdetailsDecorate::instance()
            ->where(['cid'=>$param['cid']])
            ->first();
        if(!$info || $info['value']){
            return Normal::returnCode(Normal::SUC,'请求成功',[]);
        }
        $data = json_decode($info['value'],true);
        return Normal::returnCode(Normal::SUC,'请求成功',$data);
    }
    /**
     * 商品设置
     * @param $param
     * @return array
     */
    public function goodsConfig($param)
    {
        $result = $this->validate($param);
        if($result['code'] != Normal::SUC){
            return Normal::returnCode(Normal::FAIL,$result['msg']);
        }
        $info = GoodsdetailsDecorate::instance()
            ->where(['cid'=>$param['cid']])
            ->first();
        if(!$info){
            $res = GoodsdetailsDecorate::instance()
                ->insert([
                    'sid' => $param['sid'] ?? 1,
                    'cid' => $param['cid'],
                    'value' => json_encode($param),
                    'cd' => time(),
                    'md' => time(),
                ]);
        }else{
            $res = GoodsdetailsDecorate::instance()
                ->where(['id'=>$info['id']])
                ->update([
                    'value' => json_encode($param),
                    'md' => time(),
                ]);
        }

        if(!$res){
            return Normal::returnCode(Normal::FAIL,'操作失败');
        }
        return Normal::returnCode(Normal::SUC,'操作成功');
    }

    public function validate($param)
    {
        if(!isset($param['is_show_price_market'])){
            return Normal::returnCode(Normal::FAIL,'请设置会员模式是否显示市场价');
        }

        if(!isset($param['is_show_num_sale'])){
            return Normal::returnCode(Normal::FAIL,'请设置是否显示销量');
        }

        if(!isset($param['is_show_stock_goods'])){
            return Normal::returnCode(Normal::FAIL,'请设置是否显示售罄商品');
        }

        if(!isset($param['is_show_guess_like'])){
            return Normal::returnCode(Normal::FAIL,'请设置是否显示猜你喜欢');
        }

        if(!isset($param['b2c_share_card_type'])){
            return Normal::returnCode(Normal::FAIL,'请设置b2c小程序分享卡片样式');
        }

        if(!isset($param['b2c_share_title'])){
            return Normal::returnCode(Normal::FAIL,'请设置b2c小程序分享标题');
        }

        if(!isset($param['b2b_share_card_type'])){
            return Normal::returnCode(Normal::FAIL,'请设置b2b小程序分享卡片样式');
        }

        if(!isset($param['b2b_share_title'])){
            return Normal::returnCode(Normal::FAIL,'请设置b2b小程序分享标题');
        }

        if(!isset($param['price_round_unit'])){
            return Normal::returnCode(Normal::FAIL,'请设置价格取整单位');
        }

        if(!isset($param['price_round_type'])){
            return Normal::returnCode(Normal::FAIL,'请设置价格取整模式');
        }

        return Normal::returnCode(Normal::SUC,'验证成功');
    }
}